package cn.nfwx.tv.helper;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import java.util.Stack;

import cn.nfwx.tv.util.LogUtil;

/**
 * Created by zhouzechao on 2017/12/12.
 */

public class ActivityHelper implements Application.ActivityLifecycleCallbacks {
    private static final String TAG = ActivityHelper.class.getSimpleName();
    private int mActivityCount = 0;
    private Stack<Activity> mStack = new Stack<>();

    private ActivityStatusListener activityStatusListener;
    private boolean isBackground = false;

    public void setActivityStatusListener(ActivityStatusListener activityStatusListener) {
        this.activityStatusListener = activityStatusListener;
    }

    public interface ActivityStatusListener {
        void background();

        void forground();

        void exitApp();
    }

    public void exitApp() {
        for (Activity activity : mStack) {
            if (activity != null && !activity.isFinishing()) {
                activity.finish();
            }
        }
    }


    private static ActivityHelper instance;

    public static ActivityHelper getInstance() {
        if (instance == null) {
            synchronized (AccountHelper.class) {
                if (instance == null) {
                    instance = new ActivityHelper();
                }
            }
        }
        return instance;
    }

    public Activity getCurrentActivity() {
        if (hasActivity()) {
            return mStack.lastElement();
        } else {
            return null;
        }
    }

    public boolean hasActivity() {
        return !mStack.isEmpty();
    }

    private ActivityHelper() {
        isBackground = false;
    }

    /**
     * 是否当前Activity
     *
     * @param clazzs
     * @return
     */
    public boolean isCurrentActivity(String... clazzs) {
        Activity activity = getCurrentActivity();
        for (String clazz : clazzs) {
            if (activity.getClass().getSimpleName().equals(clazz)) {
                return true;
            }
        }
        return false;
    }

    public boolean isBackground() {
        return mActivityCount < 1;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        LogUtil.d(TAG, "onActivityCreated--" + activity.getLocalClassName());
        mStack.add(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {
        LogUtil.v(TAG, "onActivityStarted--" + activity.getLocalClassName());
        mActivityCount++;

        if (!isBackground() && isBackground) {
            isBackground = false;
            activityStatusListener.forground();
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {
        LogUtil.d(TAG, "onActivityResumed--" + activity.getLocalClassName());
    }

    @Override
    public void onActivityPaused(Activity activity) {
        LogUtil.d(TAG, "onActivityPaused--" + activity.getLocalClassName());
    }

    @Override
    public void onActivityStopped(Activity activity) {
        LogUtil.d(TAG, "onActivityStopped--" + activity.getLocalClassName());
        if (isBackground()) {
            isBackground = true;
            activityStatusListener.background();
        }
        mActivityCount--;
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        LogUtil.d(TAG, "onActivitySaveInstanceState--" + activity.getLocalClassName());
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        LogUtil.d(TAG, "onActivityDestroyed--" + activity.getLocalClassName());
        mStack.remove(activity);
        if (mStack.isEmpty() && mActivityCount == 0) {
            activityStatusListener.exitApp();
        }
    }
}
